package com.kly.sc.api.application.controller.user;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.google.common.collect.Lists;
import com.kly.sc.api.application.controller.user.request.ModelsUserCodeReq;
import com.kly.sc.api.application.controller.user.response.UserOnlineRsp;
import com.kly.sc.api.application.controller.user.response.UserOnlineVO;
import com.kly.sc.api.application.service.utils.BaseComponent;
import com.kly.user.dto.UserInfoDto;
import com.kly.user.enums.OnlineStatus;
import com.kly.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/api/chat")
public class UserOnlineController extends BaseComponent {

    private static final Log logger = LogFactory.getLog(UserOnlineController.class);

    @Resource
    UserService userService;

    @ResponseBody
    @RequestMapping(value = "/model/online")
    public Response changeUserInfo(@RequestBody ModelsUserCodeReq modelsUserCodeReq) {
        final List<UserInfoDto> userInfoDtos = userService.listUserInfos(modelsUserCodeReq.getUserCodes());
        if (CollectionUtils.isEmpty(userInfoDtos)) {
            return Response.ok(UserOnlineRsp.builder().onlineUserList(Lists.newArrayList()).build());
        }

        // 只返回在线用户
        final List<UserOnlineVO> userOnlineVOList = userInfoDtos.parallelStream().filter(o -> OnlineStatus.ONLINE_STATUS.getType() == o.getOnline()).map(o -> UserOnlineVO.builder().userCode(o.getUserCode()).online(o.getOnline()).build()).collect(Collectors.toList());

        return Response.ok(UserOnlineRsp.builder().onlineUserList(userOnlineVOList).build());

    }

}
